package com.test31_IO应用;

import com.test31_IO应用.exception.ArrayLengthInvalidateException;
import com.test31_IO应用.util.InputUtil;

/**
 * 数字的业务接口
 */
interface INumberService {
    /** 获取最大值 */
    public int max();
    /** 获取最小值 */
    public int min();
}

class NumberServiceImpl implements INumberService {
    private static final int DEFAULT_LENGTH = 3;
    private int[] data;
    private int max_value;
    private int min_value;

    public NumberServiceImpl() {
        this(DEFAULT_LENGTH);
    }

    public NumberServiceImpl(int len) {
        if (len <= 0) {
            throw new ArrayLengthInvalidateException("要开辟的数据长度错误！");
        } else {
            this.data = new int[len];
        }
        this.inputData();   // 键盘输入数据
        this.handleData();  // 进行处理操作
    }

    /**
     * 实现键盘数据的输入处理
     */
    public void inputData() {
        for (int x = 0; x < this.data.length; x++) {
            this.data[x] = InputUtil.getInt("请输入第" + (x+1) + "个数据：");
        }
    }

    /** 数据处理操作，可以实现大小定义 */
    private void handleData() {
        this.max_value = this.data[0];
        this.min_value = this.data[0];
        for (int temp : this.data) {
            if (temp > this.max_value) {
                this.max_value = temp;
            }
            if (temp < this.min_value) {
                this.min_value = temp;
            }
        }
    }

    @Override
    public int max() {
        return this.max_value;
    }

    @Override
    public int min() {
        return this.min_value;
    }
}

class NumberFactory {
    private NumberFactory() {
    }

    public static INumberService getInstance(int... args) {
        if (args.length == 1) {
            return new NumberServiceImpl(args[0]);
        }
        return new NumberServiceImpl();
    }
}

/**
 * 输入数据比大小
 */
public class YootkDemo_01 {

    public static void main(String[] args) {
        INumberService numberService = NumberFactory.getInstance();
        System.out.println("【输入数据的最大值】" + numberService.max());
        System.out.println("【输入数据的最小值】" + numberService.min());
    }
}
